package com.cn.yygh.common.result;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class R {

    @ApiModelProperty(value = "是否成功")
    private Boolean success;
    @ApiModelProperty(value = "返回状态码")
    private Integer code;
    @ApiModelProperty(value = "返回消息")
    private String message;
    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data = new HashMap<>();

    //链式调用ok
    public static R ok() {
        R r = new R();
        r.setCode(ResultCode.SUCCESS);
        r.setMessage("操作成功");
        r.setSuccess(true);
        return r;
    }

    //失败返回
    public static R err() {
        R r = new R();
        r.setMessage("操作失败");
        r.setCode(ResultCode.ERROR);
        r.setSuccess(false);
        return  r;
    }

    //单独设置success 的返回值
    public R success(Boolean flag) {
        this.setSuccess(flag);
        return this;
    }

    //单独设置message
    public R message(String message) {
        this.setMessage(message);
        return this;
    }

    //单独设置code
    public R code(Integer code) {
        this.setCode(code);
        return this;
    }

    //添加一个 对象到data数据里面
    public R data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    //同时传入多个数据
    public R data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }
}
